<?php

namespace app\controller\wmapi\partner;

use app\model\WaPol;
use Illuminate\Support\Facades;
use Illuminate\Support\Facades\Validator;
use plugin\admin\app\common\Util;
use plugin\admin\app\model\Admin;
use support\Request;
use app\model\WaAdmins;
use app\model\WaRoles;
use app\model\WaRules;
use app\model\WaDepartment;
use app\model\WaAdminRoles;
use Illuminate\Support\Facades\DB;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Cache;
use Tinywan\Validate\Validate;
use app\controller\wmapi\IndexController;

class PolController extends Validate
{
    /**
     * 装货港地址管理 -列表-分页
     * @param Request $request
     * @return mixed
     */
    public function polList(Request $request)
    {
        $param = $request->all();
        $pageNum = page($param['pageNum']);
        $fields = ['*'];
        $condition = [];
        $condition [] = ['status', '!=',4];
        $page = page($param['page']);
        $rule = WaPol::list($page, $pageNum, $fields, $condition);

        return IndexController::successed($rule, '装货港地址列表');
    }

    /**
     * 装货港地址管理 -列表-不分页
     * @param Request $request
     * @return mixed
     */
    public function polAll(Request $request)
    {
//        process($request);
        $param = $request->all();
        $adminId = json_decode(adminId($request));
        $fields = ['*'];
        $condition = [];
//        if ($param['area']) {
//            $condition [] = ['area', 'like', '%' . $param['area'] . '%']; //
//        }
        $condition [] = ['status', '=',1];
        $rule = WaPol::all($fields, $condition);
        return IndexController::successed($rule, '装货港地址列表');
    }

    /**
     * 装货港地区管理 -列表-不分页
     * @param Request $request
     * @return mixed
     */
    public function WaPolAll(Request $request)
    {
//        process($request);
        $param = $request->all();
        $adminId = json_decode(adminId($request));
        $fields = ['area'];
        $condition = [];
        if ($param['country']) {
            $condition [] = ['country', '=',$param['country']]; //
        }
        if ($param['area']) {
            $condition [] = ['area', 'like', '%' . $param['area'] . '%']; //
        }
            $condition [] = ['status', '=',1];
        $rule = WaPol::all($fields, $condition);
        return IndexController::successed($rule, '装货港地区列表');
    }


    /**
     * 装货港-创建
     * @param Request $request
     * @return \support\Response
     */
    public function polCreate(Request $request)
    {
//        process($request);
        $param = $request->all();
        $adminId = json_decode(adminId($request));
        $data = [
            'country' => $param['country'],
            'area' => $param['area'],
            'created_at' => dateTime(),
        ];
        if (array_key_exists('country_en',$param)){
            $data['country_en'] = $param['country_en'];
        }
        if (array_key_exists('area_en',$param)){
            $data['area_en'] = $param['area_en'];
        }
       $pol =  WaPol::find(['country' => $param['country'],
            'area' => $param['area']]);
        $pol = json_decode($pol);
        if ($pol){
            return IndexController::errored('','该地址已存在');
        }

            \support\Db::beginTransaction();
            try {
                $res = WaPol::created($data);
                \support\Db::commit();
                return IndexController::successed($res, '装货港地址创建成功');
            } catch (\Throwable $exception) {
                IndexController::logError('polCreate', $param, $adminId, $exception);
                \support\Db::rollBack();
                return IndexController::errored($exception, '装货港地址创建失败');
            }


    }

    /**
     * 装货港-更新
     * @param Request $request
     * @return \support\Response
     */
    public function polEdit(Request $request)
    {
//        process($request);
        $param = $request->all();
        $adminId = json_decode(adminId($request));
        $data = [
            'country' => $param['country'],
            'area' => $param['area'],
            'updated_at' => dateTime(),
        ];
        if (array_key_exists('country_en',$param)){
            $data['country_en'] = $param['country_en'];
        }
        if (array_key_exists('area_en',$param)){
            $data['area_en'] = $param['area_en'];
        }
//        if ($param['status']) {
//            $data['status'] = $param['status']; // 1正常 2 禁用 4 删除
//        }
        $where = [];
        $where [] = ['country', '=', $param['country']];
        $where [] = ['area', '=', $param['area']];
        $find = WaPol::find($where);
        $find = json_decode($find);
        if ($find){
                if ($param['id'] != $find->id) {
                    return IndexController::errored('', '装货港更新失败,该国家/地区已存在');
                }
        }

            $condition = [];
            $condition [] = ['id', '=', intval($param['id'])];
            \support\Db::beginTransaction();
            try {
                $res = WaPol::updated($condition, $data);
                \support\Db::commit();
                return IndexController::successed($res, '装货港更新成功');
            } catch (\Throwable $exception) {
                IndexController::logError('polEdit', $param, $adminId, $exception);
                \support\Db::rollBack();
                return IndexController::errored($exception, '装货港更新失败');
            }
    }

    /**
     * 装货港-删除
     * @param Request $request
     * @return \support\Response
     */
    public function polDel(Request $request)
    {
//        process($request);
        $param = $request->all();
        $adminId = json_decode(adminId($request));
        $condition = [];
        $condition []= ['id', '=', $param['id']];

        $data = [
            'status' => 4,
            'updated_at' => dateTime(),
        ];
        \support\Db::beginTransaction();
        try {
            $res = WaPol::updated($condition,$data);
            \support\Db::commit();
            return IndexController::successed($res, '装货港-删除成功');
        } catch (\Throwable $exception) {
            IndexController::logError('polDel', $param, $adminId, $exception);
            \support\Db::rollBack();
            return IndexController::errored($exception, '装货港-删除失败');
        }
    }

    /**
     * 管理员-详情
     * @param Request $request
     * @return \support\Response
     */
    public function polDetail(Request $request)
    {
//        process($request);
        $param = $request->all();
        $condition = [];
        $condition [] = ['id', '=', $param['id']];
        $rule = WaPol::find($condition);
        return IndexController::successed($rule, '装货港信息');
    }


}